package com.concerneyes.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.mine.model.MyDepositModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 押金详情adapter
 */

public class MyDepositAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<MyDepositModel> depositModels;
    private OnDepositClick onDepositClick;

    public MyDepositAdapter(Context context, OnDepositClick onDepositClick) {
        this.context = context;
        this.onDepositClick = onDepositClick;
        depositModels = new ArrayList<>();
    }

    public void clear() {
        depositModels.clear();
        notifyDataSetChanged();
    }


    public void addDatas(List<MyDepositModel> manModel) {
        if (manModel.size() > 0) {
            depositModels.addAll(manModel);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return depositModels.size();
    }

    @Override
    public MyDepositModel getItem(int position) {
        return depositModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_deposit, null);
            holder.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.mDepositNum = (TextView) convertView.findViewById(R.id.deposit_num);
            holder.mRentDate = (TextView) convertView.findViewById(R.id.rent_date);
            holder.mNextRentDate = (TextView) convertView.findViewById(R.id.next_rent_date);
            holder.mIvGoods = (ImageView) convertView.findViewById(R.id.ivGoods);
            holder.mTvItemtitle = (TextView) convertView.findViewById(R.id.tvItemtitle);
            holder.mTvGoodsParam = (TextView) convertView.findViewById(R.id.tvGoodsParam);
            holder.mTvGoodsParam1 = (TextView) convertView.findViewById(R.id.tvGoodsParam1);
            holder.mLlGoodInfo = (RelativeLayout) convertView.findViewById(R.id.llGoodInfo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mTvName.setText(depositModels.get(position).getName());
        holder.mDepositNum.setText(depositModels.get(position).getDay1());
        holder.mRentDate.setText(depositModels.get(position).getDay2());
        holder.mNextRentDate.setText(depositModels.get(position).getDay3());
        holder.mLlGoodInfo.setTag(position);
        holder.mLlGoodInfo.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llGoodInfo:
                onDepositClick.onGlassesClick((Integer) v.getTag());
                break;
        }
    }

    public interface OnDepositClick {
        void onGlassesClick(int position);
    }

    class ViewHolder {
        TextView mTvName;
        TextView mDepositNum;
        TextView mRentDate;
        TextView mNextRentDate;
        ImageView mIvGoods;
        TextView mTvItemtitle;
        TextView mTvGoodsParam;
        TextView mTvGoodsParam1;
        RelativeLayout mLlGoodInfo;
    }
}
